package weaver.batchchanges.service.impl;

import com.engine.core.impl.Service;
import com.weaverboot.frame.ioc.anno.classAnno.WeaIocService;
import weaver.batchchanges.bean.BatchTasksChangesGlobalData;
import weaver.batchchanges.bean.BatchTasksChangesTempBean;
import weaver.batchchanges.bean.TaskHasChangedBean;
import weaver.batchchanges.bean.TaskLedgerPhase2Bean;
import weaver.batchchanges.cmd.*;
import weaver.batchchanges.service.BatchTasksChangesService;

import java.util.HashMap;
import java.util.List;


@WeaIocService
public class BatchTasksChangesServiceImpl extends Service implements BatchTasksChangesService {

    @Override
    public List<BatchTasksChangesTempBean> getAllBatchTasksChangesTemp() {
        List<BatchTasksChangesTempBean> result = commandExecutor.execute(new GetBatchTasksChangesTempDataCmd());
        return result;
    }

    @Override
    public List<TaskLedgerPhase2Bean> getAllTaskLedgerPhase2() {
        List<TaskLedgerPhase2Bean> result = commandExecutor.execute(new GetTaskLedgerPhase2DataCmd());
        return result;
    }

    @Override
    public List<TaskHasChangedBean> getAllTaskHasChanged() {
        List<TaskHasChangedBean> result = commandExecutor.execute(new GetTaskHasChangedDataCmd());
        return result;
    }


    @Override
    public boolean updateBatchTasksChangesTemp(BatchTasksChangesTempBean batchTasksChangesTempBean) {
        Boolean execute = commandExecutor.execute(new UpdateBatchTasksChangesTempDataCmd(batchTasksChangesTempBean));
        return execute;
    }

    @Override
    public boolean changeFailed(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        Boolean execute = commandExecutor.execute(new UpdateBatchTasksChangesTempDataCmd(new HashMap() {{
            put(BatchTasksChangesGlobalData.TEMPORARY_TASK_LEDGER_STATE_FIELD_NAME, BatchTasksChangesGlobalData.TEMPORARY_TASK_LEDGER_STATE_FAILURE_VALUE);
        }}, ids));
        return execute;
    }

    @Override
    public TaskLedgerPhase2Bean updateTaskLedgerPhase2(TaskLedgerPhase2Bean taskLedgerPhase2Bean) {
        List<TaskLedgerPhase2Bean> execute = commandExecutor.execute(new UpdateTaskLedgerPhase2DataCmd(taskLedgerPhase2Bean));
        if (!execute.isEmpty()) {
            return execute.get(0);
        }
        return null;
    }


    @Override
    public List<TaskLedgerPhase2Bean> updateTaskLedgerPhase2(List<TaskLedgerPhase2Bean> taskLedgerPhase2Beans) {
        if (taskLedgerPhase2Beans == null || taskLedgerPhase2Beans.isEmpty()) {
            return taskLedgerPhase2Beans;
        }
        List<TaskLedgerPhase2Bean> execute = commandExecutor.execute(new UpdateTaskLedgerPhase2DataCmd(taskLedgerPhase2Beans));
        return execute;
    }


    @Override
    public boolean deleteBatchTasksChangesTemp(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return true;
        }
        return commandExecutor.execute(new DeleteBatchTasksChangesTempDataCmd(ids));
    }

}
